home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: simple code, argc, argv, strcmp()
- Date: 2 Feb 1996 17:55:56 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4etj7c$bma@news.iag.net>
- References: <11f7cc$17261a.3b3@daprez>
- NNTP-Posting-Host: pm2-orl22.iag.net
- Keywords: strcmp encode decode argc argv
- X-Newsreader: WinVN 0.99.7
-
- In article <11f7cc$17261a.3b3@daprez>, otisg@panther.middlebury.edu says...
- >
- >Hi, a C question...
- >this one has been bothering ALL afternoon !
- >
- >This snippet of code is supposed to call appropriate function based on what
- >command line arguments are supplied.
- >
- >usage: <program> -e [SourceFile] RemoteFile
- > or
- > <program> -d [InFile]
- >
- >if the person doesn't use this right the program is supposed to call Usage()
- >which is not here, but it's in my code, and if the person calls the program
- >the correct way one of the two actions should be taken (encode or decode).
- >
- >Problem - I am doing something wrong and I almost always end up calling
- >Usage() even if I use the program correctly.
- >
- >#include <stdio.h>
- >#include <stdlib.h>
- >#include <string.h>
- >
- >int main (int argc, char **argv) {
- >
- > void Usage (void);
- > void Encode (int, char **);
- > void Decode (int, char **);
- >
- > if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) {
- > Usage();
- > }
- <snip>
-
- I assume you mean this to call Usage if argv[1] is not "-e" and not "-d"?
- Change the || to &&. Of course, if no argument was passed, who knows what
- will happen here. You should always test argc first to be certain that
- the argv element exists.
-
- Why are you allowing the code to continue to execute once it has been
- determined that the usage is incorrect? Try either:
-
- 1. follow each call to Usage with exit() (or have Usage call exit).
- 2. Use if/else to prevent execution of later statements.
-
- You could make this code much easier to read and troubleshoot by grouping
- the if conditions a little or using switch. Something like:
-
- if( (argc >= 3) && argv[1][0] == '-')
- if( argv[1][1] == 'e') /* might test == 'E' as well */
- if( argc == 4) /* might use >= instead */
- Encode( argc, argv);
- else
- Usage();
- else
- if( argv[1][1] == 'd')
- Decode( argc, argv); /* already tested >= 3 */
- else
- Usage();
- else
- Usage();
-
- or
-
- if( (argc >= 3) && argv[1][0] == '-')
- switch( argv[1][1])
- {
- case 'e':
- case 'E':
- if( argc == 4)
- Encode( argc, argv);
- else
- Usage();
- break;
-
- case 'd':
- case 'D':
- Decode( argc, argv);
- break;
-
- default:
- Usage();
- }
- else
- Usage();
-
- Note: The program should probably return EXIT_FAILURE, when it fails (eg
- incorrect usage). Probably the simplest way to handle this is to have
- Usage call exit(EXIT_FAILURE).
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-